####################
# Appendix: Japan  #    
# Figure 5 & 9     #
####################

###load library
library(openxlsx)
library(psych)
library(ggplot2)
library(gridExtra)
library(dtw)
library(tseries)
library(TSclust)
library(tidyverse)
library(zoo)
source("extract.R")



#######----------Figure 5: All patterns of indices (Japan)--------##########


#######################Laakso Taagepera ENPP###########################


#load data
b<-read.csv("JAPAN_FINAL.csv")
attach(b)

#percentage of ldp support
b$ldp_share<-ldp/allp
indep_jp<-100-allp



#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(effpar_ele/2)*((allp/indep_jp)*(1/effpar_ele^2))
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_LT<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)

mp_jp<-data.frame(b$macropartisanship_jp_LT,b$yearMon)
write.csv(mp_jp,"mp_jp.csv")


###-------without nested s2


#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(effpar_ele/2)*(allp/indep_jp)
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_LT_withoutnested<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)



###-----simply product the proportion of independent 


#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(effpar_ele/2)*(indep_jp)
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_LT_prindependent<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)





###########################Molinar############################


#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(enpp_np/2)*((allp/indep_jp)*(1/enpp_np^2))
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_NP<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)


###-------without nested s2


#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(enpp_np/2)*(allp/indep_jp)
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_NP_withoutnested<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)



###-----simply product the proportion of independent 


#index of macropartisanship

b$macropartisanship1<-(ldp/100)*(enpp_np/2)*(indep_jp)
describe(b$macropartisanship1)


#weighting process
b$macrop_var<-var(b$macropartisanship1,na.rm=T)
b$macroper_var<-var(b$ldp_share,na.rm=T)

b$macropartisanship_jp_NP_prindependent<-(b$macropartisanship1/b$macrop_var+b$ldp_share/b$macroper_var)/(1/b$macrop_var+1/b$macroper_var)



###constituting data frame for comparison

perLDP<-b$ldp/100

df_mp_enpp<-data.frame(perLDP,b$yearMon,
                       b$macropartisanship_jp_LT,
                       b$macropartisanship_jp_LT_withoutnested,
                       b$macropartisanship_jp_LT_prindependent,
                       b$macropartisanship_jp_NP,
                       b$macropartisanship_jp_NP_withoutnested,
                       b$macropartisanship_jp_NP_prindependent)
df_mp_enpp$b.yearMon<-as.Date(df_mp_enpp$b.yearMon, format = "%Y-%m-%d")
df_mp_enpp$b.yearMon<-as.factor(df_mp_enpp$b.yearMon)

##################mp with DRA##############################


#load data
a<-read.csv("macropartisan.csv")
attach(a)
describe(a)
ab<-na.omit(a)

#setting time-series information
varname<-ab$VARNAME
date<-as.Date(YearMon)
index<-(ab$value)/100
ncases<-NULL

#using "extract" to compute macropartisanship
output_jp<-extract(varname,date=date,index,ncases,,begindt=ISOdate(1960,1,1),
                   enddt = ISOdate(2015,1,1), unit="M",npass=2)
display(output_jp)
summary(output_jp)


df_ld_japan<-data.frame(output_jp$varname, output_jp$loadings1,output_jp$loadings2)
colnames(df_ld_japan)<-c("Party names","Loadings1","Loadings2")
print(xtable(df_ld_japan), include.rownames=FALSE)

#factor scores of 2 dimensions
mpartisan1_jp_DRA<-output_jp$latent1
mpartisan2_jp<-output_jp$latent2

yearMon<-as.Date(sprintf("%.2f.01", output_jp$period), format = "%Y.%m.%d")

###constituting data frame for comparison
df_dra_jp<-data.frame(as.factor(yearMon),mpartisan1_jp_DRA)
colnames(df_dra_jp)<-c("b.yearMon","mpartisan1_jp_DRA")

#join two data.frame
tb_jp_mpcomparison<-inner_join(df_dra_jp,df_mp_enpp,by=c("b.yearMon"))
write.csv(tb_jp_mpcomparison,"tb_jp_mcomparison.csv")


##################plot all indices#####################

japan_fig2<-tb_jp_mpcomparison 
colnames(japan_fig2)<-c( "b.yearMon","DRA","LDP","LT-nested","LT-simple","LT-indep","NP-nested","NT-simple","NP-independent")

#plot the simple macropartisanship and the modified
tidy_all_indices<-japan_fig2 %>% 
  gather(key="variable", value="value", -b.yearMon)

japan_compare<-ggplot(tidy_all_indices, aes(as.yearmon(x = b.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  theme_minimal()
ggsave(plot=japan_compare,filename="japan_compare.pdf",width=12,height=8)





#######---------Figure 9: Dynamic time warping distance for the Japan case----------########

##################DTW############################
windows(30,25)
pdf("fig_dtw_japan.pdf")
split.screen(c(3,3))

###----displaying dynamic time warping:

###LT indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_LT_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_LT_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(1)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-indep", cex.main = 0.6)


###NP indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_NP_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_NP_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(2)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-indep", cex.main = 0.6)


###LT simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_LT_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_LT_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance



screen(3)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-simple", cex.main = 0.6)


###NP simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_NP_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_NP_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(4)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-simple", cex.main = 0.6)


###LT nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_LT)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_LT

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(5)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-nested", cex.main = 0.6)


###NP nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$b.macropartisanship_jp_NP)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.b.macropartisanship_jp_NP

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(6)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-nested", cex.main = 0.6)




####DRA
#simply calulating dtw distance
df_dtw<-data.frame(tb_jp_mpcomparison$perLDP ,tb_jp_mpcomparison$mpartisan1_jp_DRA)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_jp_mpcomparison.perLDP
ts_b<-df_dtw$tb_jp_mpcomparison.mpartisan1_jp_DRA

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(7)


dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs DRA", cex.main = 0.6)


dev.off()

